package exam.model.dao;

import java.util.HashMap;
import java.util.List;

import exam.model.entity.TQuestionEntity;
import exam.system.dao.AbstractDAO;
public class TQuestionDAO extends AbstractDAO<TQuestionEntity>{
	
	public TQuestionDAO() {
		super(TQuestionEntity.class);
	}
	
	public List<TQuestionEntity> selectListQuestion() {
		return super.selectList("select q from TQuestionEntity q left outer join q.subQuestionEntity");
	}
	
	/**
	 * update: 20/5/2012
	 * @param examId
	 * @return
	 */
	public List<TQuestionEntity> select001(long examId) {
		String query = "select q from TQuestionEntity q, TQuestionExamEntity qe" +
		" where q.questionId=qe.questionId and qe.examId=:examId";
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("examId", examId);
		return jdbcManager.executeByQueryParams(query, params).getListResult();
	}
	
	/**
	 * selectBySubQuestion: (create: 21/5/2012)
	 * @param subQuestId
	 * @return
	 */
	public List<TQuestionEntity> selectBySubQuestion(long subQuestId) {
		String query = "select q from TQuestionEntity q left outer join q.subQuestionEntity " +
				"where q.subQuestId=:subQuestId";
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("subQuestId", subQuestId);
		return jdbcManager.executeByQueryParams(query, params).getListResult();
	}
	public List<TQuestionEntity> selectListQuestion(String query) {
		return super.selectList(query);
	}
	
	public TQuestionEntity selectByPk(long id) {
		return super.selectByPk(id);
	}
	
	/**
	 * insertQuestion: (update: 21/5/2012)
	 * @param question
	 * @return
	 */
	public int insertQuestion(TQuestionEntity question) {
		return super.insert(question);
	}
	
	/**
	 * updateQuestion: (update: 21/5/2012)
	 * @param tQuestionEntity
	 * @return
	 */
	public int updateQuestion(TQuestionEntity tQuestionEntity) {
		String query = "update TQuestionEntity q set q.questionContent=:questionContent, q.questionType=:questionType," +
				" q.questionMark=:questionMark, q.correctNum=:correctNum, q.subQuestId=:subQuestId, " +
				" q.updateDate=:updateDate where q.questionId=:questionId";
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("questionContent", tQuestionEntity.getQuestionContent());
		params.put("questionType", tQuestionEntity.getQuestionType());
		params.put("questionMark", tQuestionEntity.getQuestionMark());
		params.put("correctNum", tQuestionEntity.getCorrectNum());
		params.put("subQuestId", tQuestionEntity.getSubQuestId());
		params.put("updateDate", tQuestionEntity.getUpdateDate());
		params.put("questionId", tQuestionEntity.getQuestionId());
		return super.updateByQueryParams(query, params);
	}
	
	/**
	 * deleteQuestionByPk (update: 21/5/2012)
	 * @param questionId
	 * @return
	 */
//	public int deleteQuestionByPk(long questionId) {
//		String query = "delete from TQuestionEntity q where q.questionId=:questionId";
//		HashMap<String, Object> params = new HashMap<String, Object>();
//		params.put("questionId", questionId);
//		return super.deleteByQuery(query, params);
//	}
	
	public Long selectMaxQuestionId() {
		String query = "select max(q.questionId) from TQuestionEntity q";
		return (Long) super.selectByQueryParams(query, null);
	}
	
	/**
	 * deleteQuestion
	 * @param questionId
	 */
	public void deleteQuestion(long questionId) {
		super.delete(questionId);
	}
}
